Msg("Contraption Engine: Effects Library Present\n")

if (SERVER) then --Serverside Effect Dispatchers

function CDEffect_sprite_sustained(entity, offset, color, size, alpha, mode)
	local efxdata = EffectData()
	efxdata:SetEntity(entity)
	efxdata:SetOrigin(offset)
	efxdata:SetStart(Vector(color.r, color.g, color.b))
	efxdata:SetRadius(size)
	efxdata:SetMagnitude(alpha)
	efxdata:SetScale(mode)
	util.Effect( "sprite_sustained", efxdata)
end

function CDEffect_sprite_pulse(point, color, size, duration, mode, entity)
	local efxdata = EffectData()
	if (entity != nil) then efxdata:SetEntity(entity) end
	efxdata:SetOrigin(point)
	efxdata:SetStart(Vector(color.r, color.g, color.b))
	efxdata:SetRadius(size)
	efxdata:SetMagnitude(duration)
	efxdata:SetScale(mode)
	util.Effect( "sprite_flash", efxdata)
end

function CDEffect_beam_attached(entity, endpoint, color, width, duration, initalpha, mode)
	local efxdata = EffectData()
	efxdata:SetEntity(entity)
	efxdata:SetOrigin(endpoint)
	efxdata:SetStart(Vector(color.r, color.g, color.b))
	efxdata:SetRadius(width)
	local norstruct = Vector(duration, initalpha, 0)
	efxdata:SetNormal(norstruct)
	efxdata:SetScale(mode)
	util.Effect( "beam_pulse", efxdata)
end

function CDEffect_explosion_heavymissile(position)
	local efxdata = EffectData()
	efxdata:SetStart(position)
	efxdata:SetOrigin(position)
	efxdata:SetScale(1)
	util.Effect( "artillery_exp", efxdata)
end

function CDEffect_explosion_primitive(position)
	local efxdata = EffectData()
	efxdata:SetStart(position)
	efxdata:SetOrigin(position)
	efxdata:SetScale(1)
	util.Effect( "halaleffect", efxdata)
end

function CDEffect_explosion_normalmissile(position)
	local efxdata = EffectData()
	efxdata:SetStart(position)
	efxdata:SetOrigin(position)
	efxdata:SetScale(1)
	util.Effect( "big_splosion", efxdata)
end

end

if (CLIENT) then --Clientside Effect Attachers

--CORE FUNCTIONS

function CDEffect_client_processall(entity)
	if entity.effectqueue != nil then
		for key,func in pairs(entity.effectqueue) do
			func(entity, key)
		end
	end
end

function CDEffect_client_prepentity(entity) --Initializes requisite storage tables
	entity.effectqueue = entity.effectqueue or {}
	entity.Mat = entity.Mat or {}
	entity.SpriteMode = entity.SpriteMode or {}
	entity.Offset = entity.Offset or {}
	entity.Size = entity.Size or {}
	entity.Color = entity.Color or {}
end

--FUNCTION HELPERS

function CDEffect_sprite_sustained_helper(entity, paramkey)
	render.SetMaterial(entity.Mat[paramkey])
	if entity.SpriteMode[paramkey] == 7 then render.UpdateRefractTexture() end
	render.DrawSprite(entity:LocalToWorld(entity.Offset[paramkey]), entity.Size[paramkey], entity.Size[paramkey], entity.Color[paramkey])
end

--FUNCTION ASSIGNERS

function CDEffect_sprite_sustained(entity, offset, color, size, alpha, mode)
	CDEffect_client_prepentity(entity)
	local paramkey = table.insert(entity.effectqueue, CDEffect_sprite_sustained_helper)
	
	local spritemode = mode
	local matstore
	
	if (spritemode == 1) then
		matstore = Material("Sprites/animglow02")
	elseif (spritemode == 2) then
		matstore = Material("Sprites/glow01")
	elseif (spritemode == 3) then
		matstore = Material("Sprites/glow1")
	elseif (spritemode == 4) then
		matstore = Material("Sprites/glow06")
	elseif (spritemode == 5) then
		matstore = Material("Sprites/lamphalo1")
	elseif (spritemode == 6) then
		matstore = Material("Sprites/light_glow01")
	elseif (spritemode == 7) then
		matstore = Material("effects/strider_pinch_dudv")
	elseif (spritemode == 8) then
		matstore = Material("Sprites/blueflare1")
	end
	
	if (spritemode != 7) then
		matstore:SetMaterialInt("$spriterendermode",5)
		--self.Mat:SetMaterialInt("$ignorez",1)
		matstore:SetMaterialInt("$illumfactor",8)
	end
	
	entity.Mat[paramkey] = matstore
	entity.SpriteMode[paramkey] = spritemode
	entity.Offset[paramkey] = offset
	entity.Size[paramkey] = size
	entity.Color[paramkey] = Color(color.r, color.g, color.b, alpha)
end

end